﻿// CTestDlg.cpp: 实现文件
//

#include "stdafx.h"
#include "DC-QC.h"
#include "CTestDlg.h"
#include "afxdialogex.h"


// CTestDlg 对话框

IMPLEMENT_DYNAMIC(CTestDlg, CDialogEx)

CTestDlg::CTestDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_TESTDLG, pParent)
{
	m_strCurFileName=_T("");
}

CTestDlg::~CTestDlg()
{
}

void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT2, m_ctlDebug);
}


BEGIN_MESSAGE_MAP(CTestDlg, CDialogEx)
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_BN_CLICKED(IDC_BUTTON1, &CTestDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON3, &CTestDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON5, &CTestDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BTPRE, &CTestDlg::OnBnClickedBtpre)
	ON_BN_CLICKED(IDC_BTNEXT, &CTestDlg::OnBnClickedBtnext)
END_MESSAGE_MAP()


// CTestDlg 消息处理程序

void CTestDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialogEx::OnSize(nType, cx, cy);

	// TODO: 在此处添加消息处理程序代码
	CRect rect;
	GetClientRect(rect);
	if (m_wndLastCurveA.m_hWnd )
	{
		CRect rt = rect;
		rect.left = 10;
		rect.top = 190;
		rect.DeflateRect(2, 2, 2, 2);
		rt = rect;
		rt.bottom = rect.top+ rect.Height()/3;
		//m_wndLastCurveA.MoveWindow(rt);
		rt.top = rt.bottom + 10;
		rt.bottom = rect.bottom;
		m_wndLastCurveA.MoveWindow(rt);
	}
}

int CTestDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDialogEx::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	CRect rect(0, 0, 10, 10);
	m_wndLastCurveA.Create(NULL, NULL, WS_CHILD | WS_VISIBLE, rect, this, TCC_CUVE1_ID, NULL);

	m_wndLastCurveA.SetLineWidth(2);
	return 0;
}

void CTestDlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFile;
	CFileDialog dlg(true, TEXT("bcf"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT, _T("色谱数据文件(*.bcf)|*.bcf||"));
	//要加上如下的代码，要不然有选择的文件个数限制，2021-12-22 zpj
	//在打开文件时，选择多个文件
	DWORD MAXFILE = 2562; //2562 is the max
	TCHAR* pc = new TCHAR[MAXFILE * (MAX_PATH + 1) + 1];
	pc[0] = pc[1] = 0;
	dlg.m_ofn.nMaxFile = MAXFILE;
	dlg.m_ofn.lpstrFile = pc;
	if (dlg.DoModal() != IDOK) return;
	else
	{
		m_arrFiles.RemoveAll();
		POSITION pos = dlg.GetStartPosition();
		while (pos != NULL)
		{
			strFile = dlg.GetNextPathName(pos);
			m_arrFiles.Add(strFile);
		}
	}
	m_nIndex = m_arrFiles.GetCount() - 1;
	GetDlgItem(IDC_EDIT1)->SetWindowTextW(strFile);
	m_strCurFileName=strFile;

	cfTest = gl.ReadChmDate(strFile);
	m_nFileType = GetChTypeFromFileName(strFile);
	m_wndLastCurveA.UpdateGraph(cfTest, m_nFileType);
}

//重新积分
void CTestDlg::OnBnClickedButton3()
{
	// TODO: 在此添加控件通知处理程序代码
	bool bExport = FALSE;
	if (((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck() == 1)
		bExport = TRUE;
	gl.m_strDebugInfo = _T("");
	if(m_nFileType==0)
		cfTest= gl.CaculateChrm(cfTest,FALSE);
	else
		cfTest = gl.CaculateChrm(cfTest, TRUE);

	CString strTemp, strTime;
	m_ctlDebug.GetWindowTextW(strTemp);
	CTime tTime = CTime::GetCurrentTime();
	strTime.Format(_T("%4d-%2d-%2d %2d:%2d:%2d %s"), tTime.GetYear(), tTime.GetMonth(), tTime.GetDay(), tTime.GetHour(), tTime.GetMinute(), tTime.GetSecond(),m_strCurFileName);
	m_ctlDebug.SetWindowTextW(strTemp + _T("\r\n")+ strTime+ _T("\r\n") + gl.m_strDebugInfo);

	if (bExport)
	{
		CString strFile;
		CFileDialog dlg1(FALSE, TEXT("TXT"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("色谱数据文件(*.txt)|*.txt||"));
		if (dlg1.DoModal() != IDOK) return;
		strFile = dlg1.GetPathName();
		cfTest.ToTxt(strFile);
	}
}

BOOL CTestDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// TODO:  在此添加额外的初始化
	CRect rect;
	GetClientRect(rect);

	CRect rt = rect;
	rt.left = 260;
	rt.top = 60;
	rt.right = 940;
	rt.bottom = 190;
	//m_ctlDebug.MoveWindow(rt);
	return TRUE;  // return TRUE unless you set the focus to a control
				  // 异常: OCX 属性页应返回 FALSE
}

float 	CTestDlg::FindBaseVal(Chrom_File cf)
{
	float dSum = 0;
	for (int i = 600; i < 1000; i++)
	{
		dSum = dSum + cf.CA[i];
	}
	return dSum / 400;
}

float	CTestDlg::FindMaxVal(Chrom_File cf)
{
	float dMax = 0;
	for (int i = 0; i < 1100; i++)
	{
		if (dMax < cf.CA[i]) dMax = cf.CA[i];
	}
	return dMax;
}

//清空显示
void CTestDlg::OnBnClickedButton5()
{
	// TODO: 在此添加控件通知处理程序代码
	m_ctlDebug.SetWindowTextW( _T(""));
	gl.m_strDebugInfo = _T("");
}

void CTestDlg::OnBnClickedBtpre()
{
	// TODO: 在此添加控件通知处理程序代码
	bool bExport = FALSE;
	if (((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck() == 1)
		bExport = TRUE;
	if (m_arrFiles.GetCount() == 0) return;
	CString strFile;
	m_nIndex--;
	if (m_nIndex >= m_arrFiles.GetCount()) m_nIndex = m_arrFiles.GetCount() - 1;
	if (m_nIndex < 0) m_nIndex = 0;
	strFile = m_arrFiles.GetAt(m_nIndex);
	GetDlgItem(IDC_EDIT1)->SetWindowTextW(strFile);
	m_strCurFileName=strFile;

	cfTest = gl.ReadChmDate(strFile);
	m_nFileType = GetChTypeFromFileName(strFile);
	m_wndLastCurveA.UpdateGraph(cfTest, m_nFileType);
	if (bExport)
	{
		CFileDialog dlg1(FALSE, TEXT("TXT"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("色谱数据文件(*.txt)|*.txt||"));
		if (dlg1.DoModal() != IDOK) return;
		strFile = dlg1.GetPathName();
		cfTest.ToTxt(strFile);
	}
}


void CTestDlg::OnBnClickedBtnext()
{
	// TODO: 在此添加控件通知处理程序代码
	bool bExport = FALSE;
	if (((CButton*)GetDlgItem(IDC_CHECK1))->GetCheck() == 1)
		bExport = TRUE;
	CString strFile;
	m_nIndex++;
	if (m_nIndex >= m_arrFiles.GetCount()) m_nIndex = m_arrFiles.GetCount() - 1;
	strFile = m_arrFiles.GetAt(m_nIndex);
	GetDlgItem(IDC_EDIT1)->SetWindowTextW(strFile);
	m_strCurFileName=strFile;

	cfTest = gl.ReadChmDate(strFile);
	int nType = GetChTypeFromFileName(strFile);
	m_wndLastCurveA.UpdateGraph(cfTest, nType);

	if (bExport)
	{
		CFileDialog dlg1(FALSE, TEXT("TXT"), NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T("色谱数据文件(*.txt)|*.txt||"));
		if (dlg1.DoModal() != IDOK) return;
		strFile = dlg1.GetPathName();
		cfTest.ToTxt(strFile);
	}
}

